<?php

/**
 * user actions.
 *
 * @package    bolao-aabb
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class userActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->sf_guard_user_list = sfGuardUserPeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->sf_guard_user = $this->getRoute()->getObject();

    if ($request->isXmlHttpRequest())
    {
      return $this->renderPartial('user/details', array('sf_guard_user' => $this->sf_guard_user));
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isSuperAdmin());

    $this->form = new BolaoAdminUserForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new BolaoAdminUserForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $sf_guard_user = $this->getRoute()->getObject();

    $this->forward404Unless($this->getUser()->isSuperAdmin() || $this->getUser()->getGuardUser()->getId() == $sf_guard_user->getId());

    if ($this->getUser()->isSuperAdmin())
      $this->form = new BolaoAdminUserForm($sf_guard_user);
    else
      $this->form = new BolaoUserForm($sf_guard_user);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $sf_guard_user = $this->getRoute()->getObject();
    
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));

    if ($this->getUser()->isSuperAdmin())
      $this->form = new BolaoAdminUserForm($sf_guard_user);
    else
      $this->form = new BolaoUserForm($sf_guard_user);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $sf_guard_user = $this->getRoute()->getObject();
    $sf_guard_user->delete();

    $this->redirect('user/index');
  }

  public function executeChangePassword(sfWebRequest $request)
  {
    $this->form = new BolaoChangePasswordForm($this->getUser()->getGuardUser());
    
    if ($request->isMethod('post') || $request->isMethod('put'))
    {
      $this->processForm($request, $this->form);
    }
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sf_guard_user = $form->save();

      // Check if e-mail message can be set to user about his user information
      // update
      if ($sf_guard_user->getProfile()->getEmail())
      {
        // If changed password
        if ($form instanceof BolaoChangePasswordForm)
          $body = $this->getPartial('user/messageForChangedPassword', array('new_password' => $form['password']->getValue()));
        // If new user, send welcome message
        elseif ($form->isNew())
          $body = $this->getPartial('user/messageForNewUser', array('user' => $sf_guard_user, 'password' => $form['password']->getValue()));
        // Otherwise, send update message
        else
          $body = $this->getPartial('user/messageForUpdatedUser', array('user' => $sf_guard_user));
        
        $message = $this->getMailer()->compose(
            array('info@gbaabb.com.br' => 'G B AABB'),
            $sf_guard_user->getProfile()->getEmail(),
            'Dados do atleta - G B AABB'
        );
        $message->setBody($body, 'text/html');
       
        $this->getMailer()->send($message);
      }

      $this->redirect('user/show?username='.$sf_guard_user->getUsername());
    }
  }
}
